package cn.gift.controller;

import cn.gift.domain.TenantType;
import cn.gift.query.TenantTypeQuery;
import cn.gift.service.ITenantTypeService;
import cn.gift.util.AjaxResult;
import cn.gift.util.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tenantType")
@Api(tags = "租户类型管理")
public class TenantTypeController {
    @Autowired
    public ITenantTypeService tenantTypeService;


    /**
     * 保存和修改公用的
     * @param tenantType  传递的实体
     * @return AjaxResult
     */
    @PutMapping
    @ApiOperation(tags = "租户类型管理",value = "保存租户类型")
    @ApiParam(name = "tenantType",value = "TenantType",required = true,type = "TenantType")
    @ApiResponse(code = 200,message = "操作成功")
    public AjaxResult addOrUpdate(@RequestBody TenantType tenantType){
        try{
            if(tenantType.getId()!=null){
                tenantTypeService.updateById(tenantType);
            }
            else{
                tenantTypeService.insert(tenantType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(tags = "租户类型管理",value = "删除租户类型")
    @ApiParam(name = "id",value = "Long",required = true,type = "Long")
    @ApiResponse(code = 200,message = "操作成功")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            tenantTypeService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
     * 获取信息
     * @param id 用户id
     * @return AjaxResult 响应体
     */
    @GetMapping("/{id}")
    @ApiOperation(tags = "租户类型管理",value = "获取租户类型")
    @ApiParam(name = "id",value = "Long",required = true,type = "Long")
    @ApiResponse(code = 200,message = "操作成功")
    public AjaxResult get(@PathVariable("id") Long id)
    {
        try {
            TenantType tenantType = tenantTypeService.selectById(id);
            return AjaxResult.me().setResultObj(tenantType);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return AjaxResult
    */
    @GetMapping
    @ApiOperation(tags = "租户类型管理",value = "查询所有租户类型")
    @ApiResponse(code = 200,message = "操作成功")
    public AjaxResult list(){
        try {
            List< TenantType> list = tenantTypeService.selectList(null);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @ApiOperation(tags = "租户类型管理",value = "分页查询租户类型")
    @ApiParam(name = "query",value = "TenantTypeQuery",required = true,type = "TenantTypeQuery")
    @ApiResponse(code = 200,message = "操作成功")
    public AjaxResult json(@RequestBody TenantTypeQuery query)
    {
        Page<TenantType> page = new Page<TenantType>(query.getPage(),query.getRows()); //封装了当前页和每页显示多少行
        //page = tenantTypeService.selectPage(page); //传入了分页条件,进行查询返回分页数据
        //做高级查询
        ////keywork-->name,description  where name like %xxx% or description like %xxx%
        Wrapper<TenantType> wrapper = null;
        if (StringUtils.isNotBlank(query.getKeyword())){
            wrapper = new EntityWrapper<>();
            wrapper.like("name", query.getKeyword())
                    .or()
                    .like("description",query.getKeyword());
        }
        page = tenantTypeService.selectPage(page, wrapper);

        //分装mybatis plus的分页数据为我们自己PageList
        PageList pageList = new PageList<TenantType>(page.getTotal(),page.getRecords());

        return AjaxResult.me().setResultObj(pageList);
    }
}
